package lg.minecraft.plugin.pokedex.manager;

import lg.minecraft.plugin.pokedex.LGPokedex;
import lg.minecraft.plugin.pokedex.PokedexPlayer;
import lg.minecraft.plugin.pokedex.config.MainConfig;
import lg.minecraft.plugin.pokedex.storage.DataStorage;
import lg.minecraft.plugin.pokedex.storage.MYSQLStorage;
import lg.minecraft.plugin.pokedex.storage.YMALStorage;
import lg.minecraft.plugin.pokedex.util.LRUCacheMap;
import lombok.Getter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.UUID;

/**
 * @author langle__
 * @version 1.0
 */
public class PokedexPlayerManger {

    private final Map<UUID, PokedexPlayer> pokedexPlayerMap;
    @Getter private final DataStorage dataStorage;

    public PokedexPlayerManger() {
        MainConfig mainConfig = MainConfig.INSTANCE;
        int maxStorageCount = mainConfig.getMaxStorageCount();
        pokedexPlayerMap = new LRUCacheMap<>(maxStorageCount);
        dataStorage = switch (mainConfig.getStorageType()) {
            case YAML -> new YMALStorage(mainConfig.getYmlStoragePath(), mainConfig.getMaxStorageCount());
            case MYSQL -> new MYSQLStorage(mainConfig.getMySqlStorageUrl(), mainConfig.getMySqlUser(), mainConfig.getMySqlPassword());
        };
    }

    public PokedexPlayer getPokedexPlayer(@NotNull Player player) {
        PokedexPlayer pokedexPlayer = pokedexPlayerMap.get(player.getUniqueId());
        if (pokedexPlayer == null && player.isOnline()) {
            pokedexPlayer = new PokedexPlayer(player);
            addPokedexPlayer(player.getUniqueId(), pokedexPlayer);
            pokedexPlayer.loadData();
        }
        // 如果不存在，创建新的实例并放入缓存
        return pokedexPlayer;
    }

    /**
     * 判断指定的玩家是否已经存在对应的 PokedexPlayer 实例。
     *
     * @param uuid 玩家的 UUID
     * @return 如果存在返回 true，否则返回 false
     */
    public boolean containsPokedexPlayer(UUID uuid) {
        return pokedexPlayerMap.containsKey(uuid);
    }

    /**
     * 手动添加或更新一个 PokedexPlayer 实例到缓存中。
     *
     * @param uuid 玩家的 UUID
     * @param PokedexPlayer 要添加的 PokedexPlayer 实例
     */
    public void addPokedexPlayer(UUID uuid, PokedexPlayer PokedexPlayer) {
        pokedexPlayerMap.put(uuid, PokedexPlayer);
    }

    /**
     * 从缓存中移除指定玩家的 PokedexPlayer 实例。
     *
     * @param uuid 玩家的 UUID
     */
    public void removePokedexPlayer(UUID uuid) {
        PokedexPlayer pokedexPlayer = pokedexPlayerMap.remove(uuid);
        if (pokedexPlayer == null) return;
        if (dataStorage instanceof YMALStorage yml) {
            yml.removeEntry(uuid);
        }
        pokedexPlayer.saveData();
    }

    /**
     * 获取当前缓存大小（用于监控）
     */
    public int getCurrentCacheSize() {
        return pokedexPlayerMap.size();
    }

    /**
     * 清空缓存（服务器关闭时调用）
     */
    public void clearCache() {
        pokedexPlayerMap.clear();
    }

    public void saveAll() {
        if (dataStorage instanceof YMALStorage yml) {
            yml.asyncSaveAllData();
        }
    }

}
